package com.hnevc.trafficdemotest1.fragment;

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.github.mikephil.charting.charts.LineChart;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.hnevc.trafficdemotest1.R;
import com.hnevc.trafficdemotest1.entity.Weather;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.logging.SimpleFormatter;

import static com.android.volley.toolbox.Volley.newRequestQueue;

/**
 * Created by Administrator on 2018/1/2/002.
 */

public class LifeHelpFragment extends Fragment {
    View view;
    Weather mWcurrentData;
    TextView mWcurrentTv, mWcurrentTv1;
    String today;
    String [] result;
    ArrayList<String> minTemps;
    ArrayList<String> maxTemps;
    ImageView mResImg;
    LineChart mChart;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.life_help_layout, container, false);
        initView();
        initData();
        return view;
    }
    public LifeHelpFragment(){

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    private void initView() {
        mWcurrentTv = (TextView) view.findViewById(R.id.life_help_Wcurrent_tv);
        mWcurrentTv1 = (TextView) view.findViewById(R.id.life_help_Wcurrent_tv1);
        mResImg = (ImageView) view.findViewById(R.id.life_help_refresh_img);
        mChart = (LineChart) view.findViewById(R.id.chart1);
        mChart.setDescription("");//设置LineChart右下角描述信息
        mChart.setDrawBorders(false);//设置是否绘制表格
        mChart.getLegend().setEnabled(false);//设置图例是否显示
        mChart.getAxisRight().setEnabled(false);//设置右边的坐标是否显示
        mChart.animateY(2500);//设置折线图从Y轴方向按照2500毫秒的时间出现

    }
    private void initData() {
        mResImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getWeatherData();
            }
        });
    }
    private void getWeatherData(){
        final HashMap<String,String> map = new HashMap<>();
        map.put("userName", "user1");
        JSONObject object = new JSONObject(map);
        String url = "http://192.168.1.142:8080/RestaurantServer/action/getWeather.do";
        final JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, object, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                mWcurrentData = gson.fromJson(jsonObject.toString(), Weather.class);
                mWcurrentTv.setText(mWcurrentData.getWCurrent()+"\u00B0");
                List<Weather.ROWSDETAILBean> rowsdetailBeen = mWcurrentData.getROWS_DETAIL();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                Date curDate = new Date(System.currentTimeMillis());
                String curDareStr = formatter.format(curDate);
                minTemps = new ArrayList<>();
                maxTemps = new ArrayList<>();
                for (int i = 0;i<rowsdetailBeen.size();i++){
                    if (rowsdetailBeen.get(i).getWData().equals(curDareStr)){
                        today = rowsdetailBeen.get(i).getTemperature();
                        mWcurrentTv1.setText("今天"+today+"\u2103");
                    }
                    String data = rowsdetailBeen.get(i).getTemperature();
                    result =  data.split("~");
                    minTemps.add(result[0]);
                    maxTemps.add(result[1]);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getActivity(),"网络请求失败，请稍后重试",Toast.LENGTH_SHORT).show();
            }
        });
        RequestQueue requestQueue = newRequestQueue(getActivity());
        requestQueue.add(request);
    }
}
